package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostManagementMapper {
    /**
     * 创建新帖子
     * @param post 帖子实体
     * @return 插入记录数
     */
    @Insert("INSERT INTO posts (user_id, title, content, created_at, updated_at) " +
            "VALUES (#{userId}, #{title}, #{content}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "postId", keyColumn = "post_id")
    int createPost(Post post);

    /**
     * 更新帖子信息
     * @param post 帖子实体
     * @return 更新记录数
     */
    @Update("UPDATE posts SET title = #{title}, content = #{content}, updated_at = #{updatedAt} " +
            "WHERE post_id = #{postId}")
    int updatePost(Post post);

    /**
     * 删除帖子
     * @param postId 帖子 ID
     * @return 删除记录数
     */
    // 修改为逻辑删除
    @Update("UPDATE posts SET status = 0 WHERE post_id = #{postId}")
    int deletePost(Long postId);
    /**
     * 根据帖子 ID 查询帖子
     * @param postId 帖子 ID
     * @return 帖子实体
     */
    @Select("SELECT p.*, u.username, u.avatar, u.role as userRole " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.user_id " +
            "WHERE p.post_id = #{postId}")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),
            @Result(property = "viewCount", column = "view_count"),
            @Result(property = "commentCount", column = "comment_count"),
            @Result(property = "likeCount", column = "like_count"),
            @Result(property = "favoriteCount", column = "favorite_count"),
            // 添加用户信息映射
            @Result(property = "username", column = "username"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "userRole", column = "userRole")
    })
    Post getPostById(Long postId);
    @Update("UPDATE posts SET status = 1 WHERE post_id = #{postId}")
    int restorePost(Long postId);
    /**
     * 查询所有帖子
     * @return 帖子列表
     */
    @Select("SELECT " +
            "p.*, " +
            "u.username, " +
            "u.avatar, " +
            "u.role as userRole, " +
            "(SELECT COUNT(*) FROM post_comments WHERE post_id = p.post_id AND status = 1) AS commentCount " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.user_id " +
            "ORDER BY p.created_at DESC " +
            "LIMIT #{size} OFFSET #{page}")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),
            @Result(property = "viewCount", column = "view_count"),
            @Result(property = "commentCount", column = "commentCount"),
            @Result(property = "username", column = "username"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "userRole", column = "userRole")
    })
    List<Post> getAllPosts(@Param("page") int page, @Param("size") int size);
    /**
     * 搜索帖子（支持标题和内容模糊搜索）
     * @param title 标题关键词（带%通配符）
     * @param content 内容关键词（带%通配符）
     * @return 符合条件的帖子列表
     */
    @Select("SELECT " +
            "p.*, " +
            "u.username, " +
            "u.avatar, " +
            "u.role as userRole, " +
            "(SELECT COUNT(*) FROM post_comments WHERE post_id = p.post_id) AS commentCount " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.user_id " +
            "ORDER BY p.created_at DESC " +
            "LIMIT #{limit} OFFSET #{offset}")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),
            @Result(property = "viewCount", column = "view_count"),
            @Result(property = "commentCount", column = "commentCount"),
            @Result(property = "username", column = "username"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "userRole", column = "userRole")
    })
    List<Post> searchPosts(
            @Param("title") String title,
            @Param("content") String content
    );

    /**
     * 获取帖子总数
     * @return 帖子总数
     */
    @Select("SELECT COUNT(*) FROM posts WHERE status = 1")
    int getTotalPostCount();
}